﻿using KillBill.Business.Entities;
using KillBill.Business.Entities.ListEntities;
using KillBill.Business.Modules.Administration;
using KillBill.Services.DTO;
using KillBill.Services.Modules.Administration.IServices;
using KillBill.Services.Modules.Administration.SecurityServiceReference;

namespace KillBill.Services.Modules.Administration
{
    public class AdministrationService : IAdministrationService
    {
        private AdministrationManager _administrationManager;
        private SecurityServiceClient _securityService;

        public AdministrationService()
        {
            _administrationManager = new AdministrationManager();
            _securityService = new SecurityServiceClient();
        }

        #region Customer
        public CustomerList FindCustomerByLastName(string lastName, string sessionId)
        {
            var sessionDTO = _securityService.IsAuthorized(sessionId,new string[] {"Administrador", "Bodeguero", "Cajero"});
            var outDTO = new CustomerList();
            if (!sessionDTO.Success)
            {
                outDTO.Success = false;
                outDTO.Messages = sessionDTO.Messages;

            }
            else
                outDTO = _administrationManager.FindCustomerByLastName(lastName);
            return outDTO;
        }


        public CustomerList GetCustomers(string sessionId)
        {
            var list = new CustomerList();
            list.DTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador" });
            if (list.Success)
                list = _administrationManager.GetCustomers();
            return list;
        }

        public CustomerList FindCustomers(string value, string sessionId)
        {
            var list = new CustomerList();
            list.DTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador" });
            if (list.Success)
                list = _administrationManager.FindCustomers(value);
            return list;
        }

        public KillBillDTO AddCustomer(Customer customer, string sessionId)
        {
            KillBillDTO sessionDTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador" });
            KillBillDTO outDTO = new KillBillDTO();
            if (!sessionDTO.Success)
            {
                outDTO.Success = false;
                outDTO.Messages = sessionDTO.Messages;

            }
            else
                outDTO = _administrationManager.AddCustomer(customer);
            return outDTO;
        }

        public Customer UpdateCustomer(Customer customer, string sessionId)
        {

            KillBillDTO sessionDTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador", "Cajero" });
            Customer outDTO = new Customer();
            if (!sessionDTO.Success)
            {
                outDTO.Success = false;
                outDTO.Messages = sessionDTO.Messages;

            }
            else
                outDTO = _administrationManager.UpdateCustomer(customer);
            return outDTO;
        }

        public Customer FindCustomer(string customerId, string sessionId)
        {
            var outDTO = new Customer();
            KillBillDTO sessionDTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador", "Cajero" });

            if (!sessionDTO.Success)
            {
                outDTO.Success = false;
                outDTO.Messages = sessionDTO.Messages;
            }
            else
                outDTO = _administrationManager.FindCustomer(customerId);
            return outDTO;
        }

        public KillBillDTO DeleteCustomer(Customer customer, string sessionId)
        {

            KillBillDTO sessionDTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador", "Cajero" });
            KillBillDTO outDTO = new KillBillDTO();
            if (!sessionDTO.Success)
            {
                outDTO.Success = false;
                outDTO.Messages = sessionDTO.Messages;

            }
            else
                outDTO = _administrationManager.DeleteCustomer(customer);
            return outDTO;
        }
        #endregion

        #region Employee Operations

        public EmployeeList GetEmployees(string sessionId)
        {
            var list = new EmployeeList();
            list.DTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador" });
            if (list.Success)
                list = _administrationManager.GetEmployees();
            return list;
        }

        public EmployeeList FindEmployees(string value, string sessionId)
        {
            var list = new EmployeeList();
            list.DTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador" });
            if (list.Success)
                list = _administrationManager.FindEmployees(value);
            return list;
        }

        public Employee FindEmployee(string employeeId, string sessionId)
        {
            KillBillDTO sessionDTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador" });
            Employee outDTO = new Employee();
            if (!sessionDTO.Success)
            {
                outDTO.Success = false;
                outDTO.Messages = sessionDTO.Messages;
            }
            else
                outDTO = _administrationManager.FindEmployee(employeeId);
            return outDTO;
        }


        public KillBillDTO AddEmployee(Employee employee, string sessionId)
        {
            KillBillDTO sessionDTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador" });
            KillBillDTO outDTO = new Employee();
            if (!sessionDTO.Success)
            {
                outDTO.Success = false;
                outDTO.Messages = sessionDTO.Messages;
            }
            else
                outDTO = _administrationManager.AddEmployee(employee);
            return outDTO;
        }


        public Employee UpdateEmployee(Employee employee, string sessionId)
        {
            KillBillDTO sessionDTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador" });
            Employee outDTO = new Employee();
            if (!sessionDTO.Success)
            {
                outDTO.Success = false;
                outDTO.Messages = sessionDTO.Messages;
            }
            else
                outDTO = _administrationManager.UpdateEmployee(employee);
            return outDTO;
        }



        public KillBillDTO DeleteEmployee(Employee employee, string sessionId)
        {
            KillBillDTO sessionDTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador" });
            KillBillDTO outDTO = new Employee();
            if (!sessionDTO.Success)
            {
                outDTO.Success = false;
                outDTO.Messages = sessionDTO.Messages;
            }
            else
                outDTO = _administrationManager.DeleteEmployee(employee);
            return outDTO;
        }





        #endregion


        #region Application Parameters Operations

        public Parameter UpdateParameter(Parameter parameter, string sessionId)
        {
            KillBillDTO sessionDTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador" });
            Parameter outDTO = new Parameter();
            if (!sessionDTO.Success)
            {
                outDTO.Success = false;
                outDTO.Messages = sessionDTO.Messages;
            }
            else
                outDTO = _administrationManager.UpdateParameter(parameter);
            return outDTO;
        }


        #endregion


        #region Application Roles

        public EmployeeRoleList GetAplicactionRoleList(string sessionId)
        {
            KillBillDTO sessionDTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador" });
            var outDTO = new EmployeeRoleList();
            if (!sessionDTO.Success)
            {
                outDTO.Success = false;
                outDTO.Messages = sessionDTO.Messages;
            }
            else
                outDTO = _administrationManager.GetAplicactionRoleList();
            return outDTO;
        }

        public EmployeeRole AddEmployeeRole(EmployeeRole employeeRole, string sessionId)
        {
            KillBillDTO sessionDTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador" });
            EmployeeRole outDTO = new EmployeeRole();
            if (!sessionDTO.Success)
            {
                outDTO.Success = false;
                outDTO.Messages = sessionDTO.Messages;
            }
            else
                outDTO = _administrationManager.AddEmployeeRole(employeeRole);
            return outDTO;
        }


        public EmployeeRole UpdateEmployeeRole(EmployeeRole employeeRole, string sessionId)
        {
            KillBillDTO sessionDTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador" });
            EmployeeRole outDTO = new EmployeeRole();
            if (!sessionDTO.Success)
            {
                outDTO.Success = false;
                outDTO.Messages = sessionDTO.Messages;
            }
            else
                outDTO = _administrationManager.UpdateEmployeeRole(employeeRole);
            return outDTO;
        }


        public KillBillDTO DeleteEmployeeRole(EmployeeRole employeeRole, string sessionId)
        {
            KillBillDTO sessionDTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador" });
            KillBillDTO outDTO = new EmployeeRole();
            if (!sessionDTO.Success)
            {
                outDTO.Success = false;
                outDTO.Messages = sessionDTO.Messages;
            }
            //else
            //    outDTO = _administrationManager.DeleteEmployeeRole(employeeRole);
            return outDTO;
        }




        #endregion
    }
}
